package com.xdqzy.baseline.common.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;

@Slf4j
public class CommonUtils {

    protected static final String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    public static final Pattern MOBILE_PATTERN = Pattern.compile("^1\\d{10}$");

    private CommonUtils() {

    }

    public static void batchCamelToUnderline(List<String> ary) {
        if (CollectionUtils.isEmpty(ary)) {
            return;
        }
        for (int i = 0; i < ary.size(); i++) {
            String s = StringUtils.camelToUnderline(ary.get(i));
            ary.set(i, s);
        }
    }


    public static <T> void clearEmptyProperties(T param) {
        if (param != null) {
            Field[] declaredFields = param.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                try {
                    if (field.getType() == String.class && org.apache.commons.lang3.StringUtils.isBlank((String) field.get(param))) {
                        field.set(param, null);
                    }
                } catch (IllegalAccessException e) {
                    log.error("get field error", e);
                }
            }
        }
    }


    public static String buildRandomCode() {
        StringBuilder shortBuffer = new StringBuilder();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString().toUpperCase();
    }

    public static Map<String, String> convertStr2Map(String str) {
        if (org.apache.commons.lang3.StringUtils.isEmpty(str) || str.indexOf("=") <= 0) {
            return Collections.emptyMap();
        }
        String[] params = str.split(";");
        Map<String, String> result = Maps.newHashMap();
        for (String param : params) {
            String[] p = param.split("=");
            result.put(p[0], p[1]);
        }
        return result;
    }

    /**
     * 提供一种订单号生成方法 :毫秒时间戳+8为随机数
     * @return
     */
    public static String getTradeNo() {
        return DateUtils.getMillTimeStr() + RandomStringUtils.randomNumeric(8);
    }

    public static boolean isMobile(String mobile) {
        return org.apache.commons.lang3.StringUtils.isNotBlank(mobile) && MOBILE_PATTERN.matcher(mobile).find();
    }

    public static void main(String[] args) {
        System.out.println(isMobile("15851082393"));
    }


}
